//
//  MOPickerView.m
//  MOPickerView
//
//  Created by 莫业金 on 16/5/20.
//  Copyright © 2016年 moruiwei. All rights reserved.
//

#import "MOPickerView.h"
#import "MOAreaModel.h"

@interface MOPickerView()<UIPickerViewDataSource,UIPickerViewDelegate>

@property (nonatomic,assign)NSInteger indexOfProvice;//当前默认选中的省份
@property (nonatomic,assign)NSInteger indexOfCity;//当前默认选中的城市
@property (nonatomic,assign)NSInteger indexOfAddress;//当前默认选中的地区

@property (nonatomic,weak) UIPickerView *pickerView;

//@property(strong,nonatomic,readwrite) UIToolbar *inputAccessoryView;

@end


@implementation MOPickerView

+ (MOPickerView *)showInitWithFrame:(CGRect)frame PickerViewWithArray:(NSArray *)array
{

    MOPickerView *view = [[MOPickerView alloc]initWithFrame:frame];
    view.arr_data = [NSMutableArray arrayWithArray:array];
    
    return view;
}

+ (MOPickerView *)showInitWithFrame:(CGRect)frame andTextField:(id)labelView PickerViewWithArray:(NSArray *)array{
    
    MOPickerView *pickerView = [[MOPickerView alloc] init];
    [labelView addSubview:pickerView];
    pickerView.arr_data = [NSMutableArray arrayWithArray:array];
    return pickerView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self == [super initWithFrame:frame]) {
    //添加pickerView
    CGFloat Screen_w = [UIScreen mainScreen].bounds.size.width;
    CGFloat Screen_h = [UIScreen mainScreen].bounds.size.height;
    UIPickerView *pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, Screen_h-216, Screen_w, 216)];
    pickerView.backgroundColor = [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1];
    self.inputView = pickerView;
    self.textAlignment = NSTextAlignmentCenter;
    self.pickerView = pickerView;
    pickerView.delegate = self;
        
    self.tintColor = [UIColor clearColor];
    }
    return self;
}
- (UIToolbar *)inputAccessoryView
{
     if(!_inputAccessoryView)
     {
     CGFloat Screen_w = [UIScreen mainScreen].bounds.size.width;
     CGFloat Screen_h = [UIScreen mainScreen].bounds.size.height;
         
       _inputAccessoryView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, Screen_h-216-44, Screen_w, 44)];
      //给工具条添加按钮
      UIBarButtonItem *item0=[[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(clickToolCancel) ];
      UIBarButtonItem *item2=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
      UIBarButtonItem *item3=[[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(clickToolOK)];
      _inputAccessoryView.items = @[item0, item2, item3];
         
         
         
         
         UILabel *lableTitle = [[UILabel alloc] initWithFrame:CGRectMake((Screen_w - 100) / 2, 0, 100, 44)];
         lableTitle.text = self.title;
         lableTitle.textAlignment = NSTextAlignmentCenter;
         lableTitle.textColor = [UIColor blackColor];
     
         [_inputAccessoryView addSubview:lableTitle];
         
     }
     return _inputAccessoryView;
 }
- (void)clickToolCancel
{
     [self resignFirstResponder];    
}
- (void)clickToolOK
{
    NSMutableArray *arr_text = [NSMutableArray array];
    
    for (int i = 0; i < self.arr_data.count; i++) {
        if (i == 0) {
            //1.获取当前的列
            NSArray *arayM = self.arr_data[0];
            NSString *str_provice = arayM[self.indexOfProvice];
            [arr_text addObject:str_provice];
        }
        if (i == 1) {
            NSArray *arayM1 = self.arr_data[1];
            NSString *str_city = arayM1[self.indexOfProvice][self.indexOfCity];
            [arr_text addObject:str_city];
        }
        if (i == 2) {
            NSArray *arayM2 = self.arr_data[2];
            NSString *str_address = arayM2[self.indexOfProvice][self.indexOfCity][self.indexOfAddress];
            [arr_text addObject:str_address];
        }
    }
    self.text = @"";
    for (NSString *str_address in arr_text) {
        self.text = [NSString stringWithFormat:@"%@  %@",self.text,str_address];
    }
    
    self.didChangePickerView(arr_text);
    
    [self clickToolCancel];
}
-(BOOL)canBecomeFirstResponder
{
    return YES;
}
#pragma mark- 设置数据
//一共多少列
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
     return self.arr_data.count;
}

//每列对应多少行
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
     //1.获取当前的列
     NSArray *arayM= self.arr_data[component];
    
    NSLog(@"%lu",(unsigned long)arayM.count);
     if (component == 1) {
        //2.返回当前列对应的行数
        return [arayM[self.indexOfProvice] count];
     }
    if (component == 2){
        //2.返回当前列对应的行数
        if ([arayM[self.indexOfProvice] count] > 0) {
            if ([arayM[self.indexOfProvice] count] > self.indexOfCity) {
                return [arayM[self.indexOfProvice][self.indexOfCity] count];
            }
        }
    }
     //2.返回当前列对应的行数
     return arayM.count;
}
//每列每行对应显示的数据是什么
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return @"";
//    //1.获取当前的列
//    NSArray *arayM = self.arr_data[component];
//    
//    
//    switch (component) {
//        case 0:
//            //2.获取当前列对应的行的数据
//            return arayM[row];
//            break;
//        case 1:{
//            if (arayM.count > 0) {
//            NSString *name = arayM[self.indexOfProvice][row];
//            if (name == nil) {
//                name = @"";
//            }
//            return name;
//            break;
//            }
//        }
//        case 2:{
//            if (arayM.count > 0) {
//                NSString *name = arayM[self.indexOfProvice][self.indexOfCity][row];
//                if (name == nil) {
//                    name = @"";
//                }
//                return name;
//                break;
//            }}
//        default:
//            return  @"";
//            break;
//    }
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view

{
    
    UILabel *myView = nil;
    //1.获取当前的列
    NSArray *arayM = self.arr_data[component];
    
    if (component == 0) {
        myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 100, 30)];
        myView.textAlignment = UITextAlignmentCenter;
        myView.text = arayM[row];
        myView.font = [UIFont systemFontOfSize:13];         //用label来设置字体大小
        
    }
    else if (component == 1){
        myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 100, 30)];
        myView.textAlignment = UITextAlignmentCenter;
        if (arayM.count > 0) {
            NSString *name = arayM[self.indexOfProvice][row];
            if (name == nil) {
                name = @"";
            }
        myView.text = name;
        }
        myView.font = [UIFont systemFontOfSize:14];         //用label来设置字体大小
        
    }
    else if (component == 2){
            myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 100, 30)];
        if (arayM.count > 0) {
            //2.返回当前列对应的行数
            if ([arayM[self.indexOfProvice] count] > 0) {
                if ([arayM[self.indexOfProvice] count] > self.indexOfCity) {
                    NSString *name = arayM[self.indexOfProvice][self.indexOfCity][row];
                    if (name == nil) {
                        name = @"";
                    }
                    myView.text = name;
                }
            }
        }
            myView.textAlignment = UITextAlignmentCenter;
            myView.font = [UIFont systemFontOfSize:14];
        }
    
    return myView;
    
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    
    if (component == 0) {//第一列省的选中改变后，就要更新第二列数据
        //更新选中省份的索引
        self.indexOfProvice = row;
        self.indexOfCity = 0;
    }
    if (component == 1) {
        //更新选中省份的索引
        self.indexOfCity = row;
    }
    if (component == 2) {
        //更新选中省份的索引
        self.indexOfAddress = row;
    }
    for (int i = 0; i < self.arr_data.count; i++) {
        if (i > component) {
            //不管之前第二列选中第几行，重新刷新数据后，都显示每二列的第一行
            [pickerView reloadComponent:i];
            [pickerView selectRow:0 inComponent:i animated:YES];
            
        }
    }
    
    NSMutableArray *arr_text = [NSMutableArray array];
    
    for (int i = 0; i < self.arr_data.count; i++) {
        if (i == 0) {
            //1.获取当前的列
            NSArray *arayM = self.arr_data[0];
            NSString *str_provice = arayM[self.indexOfProvice];
            [arr_text addObject:str_provice];
        }
        if (i == 1) {
            NSArray *arayM1 = self.arr_data[1];
            NSString *str_city = arayM1[self.indexOfProvice][self.indexOfCity];
            [arr_text addObject:str_city];
        }
        if (i == 2) {
            NSArray *arayM2 = self.arr_data[2];
            NSString *str_address = arayM2[self.indexOfProvice][self.indexOfCity][self.indexOfAddress];
            [arr_text addObject:str_address];
        }
    }
    self.text = @"";
    for (NSString *str_address in arr_text) {
        self.text = [NSString stringWithFormat:@"%@  %@",self.text,str_address];
    }
    
    self.didChangePickerView(arr_text);
   
}
@end
